This object gives you access to the on-board accelerometer.
此对象允许您访问板载加速计。
By default MicroPython sets the accelerometer range to +/- 2000 mg (g being a unit of acceleration based on the standard gravity), which configures the maximum and minimum values returned by the accelerometer functions. The range can be changed via microbit.accelerometer.set_range().
默认情况下,MicroPython 将加速度计范围设置为 +/- 2000 mg(g 是基于标准重力的加速度单位),它配置了加速度计函数返回的最大值和最小值。可以通过 更改 microbit.accelerometer.set_range() 范围 。
The accelerometer also provides convenience functions for detecting gestures. The recognised gestures are represented as strings: up, down, left, right, face up, face down, freefall, 3g, 6g, 8g, shake.
该加速度计还提供了用于检测手势的便捷功能。 识别的手势表示为字符串: 向上 、 向下 、 向左 、 向右 、 正面朝上 、 自由落体 ,3g、6g、8g, 摇晃 。
Note 注意
Gestures are not updated in the background so there needs to be constant calls to some accelerometer method to do the gesture detection. Usually gestures can be detected using a loop with a small microbit.sleep() delay.
手势不会在后台更新,因此需要有常量 调用某些 Accelerometer 方法以执行手势检测。 通常,可以使用带有少量的 microbit.sleep() 延迟。
The acceleration measurement in the x axis in milli-g, as a positive or negative integer.
x 轴上的加速度测量值,以 milli-g 为单位,以正整数或负整数表示。
Returns 返回:
The acceleration measurement in the y axis in milli-g, as a positive or negative integer.
y 轴上的加速度测量值,以 milli-g 为单位,以正整数或负整数表示。
The acceleration measurement in the z axis in milli-g, as a positive or negative integer.
z 轴上的加速度测量值,以 milli-g 为单位,以正整数或负整数表示。
The acceleration measurements in all axes at once, as a three-element tuple of integers ordered as X, Y, Z.
同时在所有轴上进行加速度测量,作为按 X、Y、Z 排序的三元素整数元组。
Get the acceleration measurement of all axes combined, as a positive integer. This is the Pythagorean sum of the X, Y and Z axes.
获取所有轴的加速度测量值,以正整数表示。这是 X、Y 和 Z 轴的毕达哥拉斯和。
The combined acceleration strength of all the axes, in milli-g.
所有轴的总加速度强度,单位为 milli-g。
String with the name of the current gesture.
String 与当前手势的名称。
name – String with the name of the gesture to check.
name – 包含要检查的手势名称的字符串。
Boolean indicating if the named gesture is currently active.
布尔值,指示命名手势当前是否处于活动状态。
name – String with the name of the gesture to check.
name – 包含要检查的手势名称的字符串。
Boolean indicating if the named gesture was active since the last call.
布尔值,指示命名手势自上次调用以来是否处于活动状态。
Get a historical list of the registered gestures.
获取已注册手势的历史列表。
Calling this function clears the gesture history before returning.
调用此函数会在返回之前清除手势历史记录。
A tuple of the gesture history, most recent is listed last.
手势历史记录的元组,最新的元组列在最后。
Set the accelerometer sensitivity range, in g (standard gravity), to the closest values supported by the hardware, so it rounds to either 2, 4, or 8 g.
将加速度计灵敏度范围(以 g(标准重力)为单位)设置为 hardware 支持的 closest 值,因此它会四舍五入为 2 克、4 克或 8 克。
value – New range for the accelerometer, an integer in g.
value (值 ) – 加速度计的新范围,以 g 为单位的整数。
A fortune telling magic 8-ball. Ask a question then shake the device for an answer.
一个算命的神奇 8 球。提出一个问题,然后摇动设备以获得答案。
# Magic 8 ball by Nicholas Tollervey. February 2016.
#
# Ask a question then shake.
#
# This program has been placed into the public domain.
from microbit import *
import random
answers = [
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes, definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
]
while True:
display.show('8')
if accelerometer.was_gesture('shake'):
display.clear()
sleep(1000)
display.scroll(random.choice(answers))
sleep(10)
Simple Slalom. Move the device to avoid the obstacles.
简单回转。移动设备以避开障碍物。
# Simple Slalom by Larry Hastings, September 2015
#
# This program has been placed into the public domain.
import microbit as m
import random
p = m.display.show
min_x = -1024
max_x = 1024
range_x = max_x - min_x
wall_min_speed = 400
player_min_speed = 200
wall_max_speed = 100
player_max_speed = 50
speed_max = 12
while True:
i = m.Image('00000:'*5)
s = i.set_pixel
player_x = 2
wall_y = -1
hole = 0
score = 0
handled_this_wall = False
wall_speed = wall_min_speed
player_speed = player_min_speed
wall_next = 0
player_next = 0
while True:
t = m.running_time()
player_update = t >= player_next
wall_update = t >= wall_next
if not (player_update or wall_update):
next_event = min(wall_next, player_next)
delta = next_event - t
m.sleep(delta)
continue
if wall_update:
# calculate new speeds
speed = min(score, speed_max)
wall_speed = wall_min_speed + int((wall_max_speed - wall_min_speed) * speed / speed_max)
player_speed = player_min_speed + int((player_max_speed - player_min_speed) * speed / speed_max)
wall_next = t + wall_speed
if wall_y < 5:
# erase old wall
use_wall_y = max(wall_y, 0)
for wall_x in range(5):
if wall_x != hole:
s(wall_x, use_wall_y, 0)
wall_reached_player = (wall_y == 4)
if player_update:
player_next = t + player_speed
# find new x coord
x = m.accelerometer.get_x()
x = min(max(min_x, x), max_x)
# print("x accel", x)
s(player_x, 4, 0) # turn off old pixel
x = ((x - min_x) / range_x) * 5
x = min(max(0, x), 4)
x = int(x + 0.5)
# print("have", position, "want", x)
if not handled_this_wall:
if player_x < x:
player_x += 1
elif player_x > x:
player_x -= 1
# print("new", position)
# print()
if wall_update:
# update wall position
wall_y += 1
if wall_y == 7:
wall_y = -1
hole = random.randrange(5)
handled_this_wall = False
if wall_y < 5:
# draw new wall
use_wall_y = max(wall_y, 0)
for wall_x in range(5):
if wall_x != hole:
s(wall_x, use_wall_y, 6)
if wall_reached_player and not handled_this_wall:
handled_this_wall = True
if (player_x != hole):
# collision! game over!
break
score += 1
if player_update:
s(player_x, 4, 9) # turn on new pixel
p(i)
p(i.SAD)
m.sleep(1000)
m.display.scroll("Score:" + str(score))
while True:
if (m.button_a.is_pressed() and m.button_a.is_pressed()):
break
m.sleep(100)